package com.mj.mafeed.activity;

import java.util.ArrayList;
import java.util.List;

import com.mj.mafeed.R;
import com.mj.mafeed.adapter.BookLogAdapter;
import com.mj.mafeed.bean.BookLogJSON;
import com.mj.mafeed.bean.BookLogListJSON;
import com.mj.mafeed.service.BookLogService;
import com.mj.mafeed.util.NetUtils;
import com.mj.mafeed.util.SPUtils;
import com.mj.mafeed.widget.LoadingDialog;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的预约
 * @author zhaominglei
 * @date 2016-3-1
 * 
 */
public class BookLogListActivity extends Activity implements OnClickListener {
	@SuppressWarnings("unused")
	private static final String TAG = BookLogListActivity.class.getSimpleName();
	private BookLogService bookLogService = new BookLogService();
	private ImageView goHome;
	private LoadingDialog loadingDialog; //加载框
	private TextView emptyHint;
	private ListView booklogItemView;
	private BookLogAdapter bookLogAdapter;
	private List<BookLogJSON> bookLogs = new ArrayList<BookLogJSON>();
	private String referer; // 来自那个链接
	private String username; //用户名
	private String sign; //签名
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_booklog_list);
		
		init();
	}
	
	private void init() {
		goHome = (ImageView) findViewById(R.id.booklog_list_gohome);
		emptyHint = (TextView) findViewById(R.id.booklog_list_empty_hint);
		booklogItemView = (ListView) findViewById(R.id.booklog_list_item_listview);

		goHome.setOnClickListener(this);
		bookLogAdapter = new BookLogAdapter(this, bookLogs);
		booklogItemView.setAdapter(bookLogAdapter);
		
		emptyHint.setVisibility(View.INVISIBLE);
		referer = getIntent().getStringExtra("referer");
		username = (String)SPUtils.get(getApplicationContext(), "username", "");
		sign = (String)SPUtils.get(getApplicationContext(), "sign", "");
		
		getBookLogs();
	}

	private void getBookLogs() {
		if (NetUtils.isConnected(getApplicationContext())) {
			loadingDialog = new LoadingDialog(this);
			loadingDialog.show();
			new BookLogListAsyncTask().execute("");
		} else {
			Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.booklog_list_gohome:
			finish();
			break;

		default:
			break;
		}
	}
	
	public class BookLogListAsyncTask extends AsyncTask<String, String, BookLogListJSON> {
		@Override
		protected BookLogListJSON doInBackground(String... params) {
			return bookLogService.getBookLog(referer, username, sign);
		}

		@Override
		protected void onPostExecute(BookLogListJSON result) {
			loadingDialog.dismiss();
			if (result != null && result.getRecordList() != null && result.getRecordList().size() > 0) {
				emptyHint.setVisibility(View.INVISIBLE);
				bookLogAdapter.setBookLogs(result.getRecordList());
				bookLogAdapter.notifyDataSetChanged();
			} else {
				emptyHint.setVisibility(View.VISIBLE);
			}
		}
	}
}
